package teacher.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import teacher.bean.Flow;
import teacher.bean.Paper;
import teacher.bean.Treatise;
import teacher.bean.Teacher;
import teacher.service.flow;
import teacher.service.treatise;
import tools.StringDate;

import com.opensymphony.xwork2.ActionSupport;

public class Uptreatise extends ActionSupport{
	
	private static final long serialVersionUID = 1L;
	private Treatise treatise;
	private File fileCom;
	private String fileComFileName;
	private String uploadPath;
	private String FilePath;
	private String dates;
	String suc;

	public String getSuc() {
		return suc;
	}

	public void setSuc(String suc) {
		this.suc = suc;
	}

	public String getDates() {
		return dates;
	}

	public void setDates(String dates) {
		this.dates = dates;
	}

	public String getFileComFileName() {
		return fileComFileName;
	}

	public void setFileComFileName(String fileComFileName) {
		this.fileComFileName = fileComFileName;
	}

	public File getFileCom() {
		return fileCom;
	}

	public void setFileCom(File fileCom) {
		this.fileCom = fileCom;
	}

	public Treatise getTreatise() {
		return treatise;
	}

	public void setTreatise(Treatise treatise) {
		this.treatise = treatise;
	}

	public String execute() throws Exception {
		
		if(treatise==null){
			return ERROR;
			
		}
		treatise.setState(0);
		HttpServletRequest request=ServletActionContext.getRequest();
		Teacher tea=(Teacher)request.getSession().getAttribute("teacher");
		int teaId=tea.getTeaId();
		treatise.setTeaId(teaId);

		if(dates!=null&&dates.length()>0)
		{
			treatise.setDate(new StringDate().StringToDate(dates));
		}
		else
		{
			treatise.setDate(new Date());
		}
		
		if(fileCom!=null)
		{
			
			InputStream is = new FileInputStream(fileCom);

			uploadPath = ServletActionContext.getServletContext()
					.getRealPath("/upload");

			File dir=new File(uploadPath);  

	        //如果这个目录不存在,则创建它  
	        if (!dir.exists()) {  
	            dir.mkdir();  
	        } 
	            FilePath=uploadPath+File.separator+this.getFileComFileName();

	     	    File toFile = new File(uploadPath, this.getFileComFileName());

	     		OutputStream os = new FileOutputStream(toFile);

	     		byte[] buffer = new byte[4096];

	     		int length = 0;

	     		while ((length = is.read(buffer)) > 0) {
	     			os.write(buffer, 0, length);
	     		}
	     		System.out.println("上传文件名"+fileComFileName);
	     		System.out.println(FilePath);

	     		is.close();

	     		os.close();
			
	     		treatise.setUpload(fileComFileName);
		}
		if(new treatise().insertTreatise(treatise)) {
			this.setSuc("success");
			
//			流程管理部分
			Treatise p=new Treatise();
			System.out.println(this.getTreatise().getName());
			p=new flow().selectTreatiseByName(this.getTreatise().getName().toString());
			System.out.println(p.getTreId()+"aaa");
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Flow f=new Flow();
			f.setFruitId(p.getTreId());
			f.setFruitKind(2);
			f.setDirDo(2);
			f.setDirTime(df.format(new Date()));
			f.setSubDo(2);
			f.setSubTime(df.format(new Date()));
			f.setTeaDo(0);
			f.setTeaTime(df.format(new Date()));
//			流程管理部分尾
			if(new flow().insertFlow(f)){
				return SUCCESS;		
			}else{
				return SUCCESS;		
			}
		} else {
			return ERROR;
		}
	}

}

